from django.shortcuts import render
from StudentManage.models import *
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.db.utils import IntegrityError
from django.http import HttpResponseNotFound, HttpResponseServerError, HttpResponseForbidden, JsonResponse
from django.views.decorators.csrf import csrf_exempt

# 主页
def index(request):
    student_list = Student.objects.all()
    return render(request, 'index.html', {'all': student_list})

# 学生信息详情页面
def info(request, ID):
    try:
        s = Student.objects.get(studentID=ID)
    except ObjectDoesNotExist:
        return HttpResponseNotFound()
    return render(request, 'info.html', {'s': s})

# 按照不同排序展示学生信息的页面
def archieve(request):
    i = Student.objects.order_by('studentID')
    n = Student.objects.order_by('name')
    a = Student.objects.order_by('age')
    se = Student.objects.order_by('sex')
    sc = Student.objects.order_by('score')

    s_list = [i, n, a, se, sc]

    return render(request, 'archieve.html', {'lists': s_list})

# Api List
# 添加数据模型的API
@csrf_exempt
def api_add(request):
    if request.is_ajax():
        if request.method == 'POST':
            studentID = request.POST['studentID']
            name = request.POST['name']
            age = request.POST['age']
            sex = request.POST['sex']
            score = request.POST['score']

            try:
                obj = Student.objects.get(studentID=studentID)
                return JsonResponse({'status': '学号为:' + studentID + '的学生已存在'})
            except MultipleObjectsReturned:
                return JsonResponse({'status': '存在意外的重复学生数据'})
            except ObjectDoesNotExist:
                try:
                    obj = Student.objects.create(studentID=studentID, name=name, age=age, sex=sex, score=score)
                    return JsonResponse({'status': '学生对象创建成功'})
                except IntegrityError:
                    return JsonResponse({'status': '学生对象创建时出现问题'})
        else:
            return HttpResponseForbidden()
    else:
        return HttpResponseServerError()

# 修改学生数据的API
@csrf_exempt
def api_change(request):
    if request.is_ajax():
        if request.method == 'POST':
            studentID = request.POST['studentID']
            name = request.POST['name']
            age = request.POST['age']
            sex = request.POST['sex']
            score = request.POST['score']

            s = Student.objects.filter(studentID=studentID)
            if len(s) > 1:
                return JsonResponse({'status': '存在意外的重复学生数据'})
            if len(s) == 1:
                s.update(name=name, age=age, sex=sex, score=score)
                return JsonResponse({'status': '学号为:' + studentID + '修改成功'})
            if len(s) == 0:
                return JsonResponse({'status': '学号为:' + studentID + '的学生不存在'})
        else:
            return HttpResponseForbidden()
    else:
        return HttpResponseServerError()

# 删除学生的API
@csrf_exempt
def api_delete(request):
    if request.is_ajax():
        if request.method == 'POST':
            studentID = request.POST['studentID']

            s = Student.objects.filter(studentID=studentID)
            if len(s) > 1:
                return JsonResponse({'status': '存在意外的重复学生数据'})
            if len(s) == 1:
                s.delete()
                return JsonResponse({'status': '学号为:' + studentID + '已删除'})
            if len(s) == 0:
                return JsonResponse({'status': '学号为:' + studentID + '的学生不存在'})
        else:
            return HttpResponseForbidden()
    else:
        return HttpResponseServerError()

# 查询学生信息的API
@csrf_exempt
def api_search(request):
    if request.is_ajax():
        if request.method == 'POST':
            studentID = request.POST['studentID']

            s = Student.objects.filter(studentID=studentID)
            if len(s) > 1:
                return JsonResponse({'status': '存在意外的重复学生数据'})
            if len(s) == 1:
                stu = s[0]
                message = '学号: ' + stu.studentID + ', 姓名: ' + stu.name + ', 年龄: ' + str(stu.age) \
                          + ', 性别: ' + stu.sex + ', 成绩: ' + str(stu.score)
                return JsonResponse({'status': message})
            if len(s) == 0:
                return JsonResponse({'status': '学号为:' + studentID + '的学生不存在'})
        else:
            return HttpResponseForbidden()
    else:
        return HttpResponseServerError()